import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTasklistStore = defineStore('tasklist', () => {

  var tasklist = ref([])


// 新增任务
var addSas= (payload)=>{
  tasklist.value.push({id:tasklist.value.length+1,name:payload,status:false});
}

// 更改任务转态
var addstatus =(payload)=>{
  // 先查找任务
  var task  = tasklist.value.find((item)=>{
    return item.id == payload
  })
  if(task){
    task.status =  !task.status
  }
}


//删除任务 
var dellist= (payload)=>{
  // 先查找
  var index = tasklist.value.findIndex((item)=>{
    return item.id == payload
  })
  if(index != -1){
    tasklist.value.splice(index,1)
  }
}


// 统计未完成的任务数量
var unlist =computed(()=>{
  return tasklist.value.filter((item)=>!item.status).length
})


  return {
    tasklist,
    addSas,
    addstatus,
    dellist,
    unlist
   }
})
